package net.kts.attraction.subway.smallpass.process.check;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.ProgressDialog;
import android.os.Handler;
import android.util.Log;

import net.kts.attraction.subway.smallpass.model.LastPath;
import net.kts.attraction.subway.smallpass.model.PassLine;
import net.kts.attraction.subway.smallpass.model.PassStation;

public class SmallWayChecker {
	
	public static List<Thread> smallWayThreadList = new ArrayList<Thread>();
	public static List<Runnable> smallWayRunnableList = new ArrayList<Runnable>();
	public static ExecutorService executorService = Executors.newFixedThreadPool(50);
	public static StationLineChecker stationLineChecker = null;
	public static MultiLineChecker multiLineChecker = null;
	public static TimeChecker timeChecker = null;
	public static ProgressDialog progressDialog = null;
	
	public static Handler uiCallBack = null;
	public static boolean startFlag = true;
	
	private static LastPath glastPath = null;
	
	// üũ!
	public static boolean checkLastPath(LastPath lastPath) {
		
		if(glastPath == null) {
			return true;
		}
		
		// ����ȭ �Լ�!
		synchronized (glastPath) {
			Log.d("kts_lastPath", "lastPath check time :" + lastPath.getLastTime() + " passStationCount : " + lastPath.getPassStationList().size());
			
 			if(lastPath.isTheEndFlag()) {
				if(glastPath.getLastTime() > lastPath.getLastTime()) {
					Log.d("kts_lastPath", "lastPath change = " + glastPath.getLastTime() + " - > " + lastPath.getLastTime());
					glastPath = lastPath;
					
					return true;			 	
				}
			}
			
			else {
				if(glastPath.getLastTime() < lastPath.getLastTime()) {
//					glastPath = lastPath;
//					Log.d("kts_lastPath", "lastPath change = " + glastPath.getLastTime() + " - > " + lastPath.getLastTime());
					return false;			 	
				}
			}
			
			
			
		}
		
		
		return true;
	}
	
	public static synchronized void arrivedTargetStation(LastPath lastPath) {
		
		if(glastPath == null) {
			glastPath = lastPath;
			Log.d("kts_lastPath", "lastPath first - time : " + glastPath.getLastTime() + " passStationcount : " +glastPath.getPassStationList().size());
		}
		
		else {
//			synchronized (glastPath) {
				
				checkLastPath(lastPath);
				
				
//			}
		}
		
		
		
		
	}
	
	public static void removeThread(Thread thread) {
		synchronized (smallWayThreadList) {
			smallWayThreadList.remove(thread);
			threadCountCheck();
		}
		
		
	}
	
	public static void removeRunnable(Runnable runnable) {
		synchronized (smallWayThreadList) {
			smallWayRunnableList.remove(runnable);
			runnableCountCheck();
		}
		
		
	}
	
	public static void runnableCountCheck() {



		if(smallWayRunnableList.size() == 0  && startFlag) {

			startFlag = false;
			//			progressDialog.dismiss();
			uiCallBack.sendEmptyMessage(0);

			String temp = "nothing";
			if(glastPath != null) {
				temp = "";
				temp += "LastPath = \r\n time :" + glastPath.getLastTime() + "\r\n";
				temp += " PassStation : " + "\r\n";
				for(int indexI = 0; indexI < glastPath.getPassStationList().size(); ++indexI) {
					PassStation passStation = glastPath.getPassStationList().get(indexI);
					temp += " passStation :" + indexI + "_" + passStation.getStationCode() + "\r\n";
					temp += " lineNum :" + passStation.getLineNum() + "\r\n";
					temp += " time" + passStation.getTime() + "\r\n";
				}
				temp += " passLineList : " + "\r\n";
				for(int indexI = 0 ; indexI < glastPath.getPassLineList().size(); ++indexI) {
					PassLine line = glastPath.getPassLineList().get(indexI);
					temp += " StationCode_" + line.getStationCode() + "->" + line.getTargetStationCode() + "\r\n passLine_" + indexI + "_" + line.getLineNum()+"-->" +line.getDirection() + "_time :" +line.getTime() + "\r\n";
				}
			}
			Log.d("kts_end", temp);
		}
	}
	
	
	public static void threadCountCheck() {
			
		if(smallWayThreadList.size() == 0 ) {
			
				progressDialog.dismiss();
				uiCallBack.sendEmptyMessage(0);
				
				String temp = "nothing";
				if(glastPath != null) {
					temp = "";
					temp += "LastPath = time :" + glastPath.getLastTime();
					temp += " PassStation : ";
					for(int indexI = 0; indexI < glastPath.getPassStationList().size(); ++indexI) {
						temp += " passStation_" + indexI + "_" + glastPath.getPassStationList().get(indexI).getStationCode();
					}
				}
				
				Log.d("kts_end", temp);
		}
		
	}
	
	
	public static LastPath getLastPath() {
		return glastPath;
	}

	public static void setGlastPath(LastPath glastPath) {
		SmallWayChecker.glastPath = glastPath;
	}
	

}
